import http from '@/server/request.js';
import * as Utils from '@/utils/index.js';
import * as Auth from '@/utils/auth';

const baseUrl = 'https://api.wxls.pro/cms';

/**
 * 根据日期、业务、用户查询阅读数量
 * buzId：业务id，路径传参， path 必传；
 * dateInt：查询时间，格式：yyyyMMdd，非必传，默认当前日期 query
 * userId：用户id，非必传，默认当前用户(需jwt token) query
 */
export const isComplete = async (param) => {
  let header = {
    auth: true,
  };
  const data = await http.request({
    baseUrl,
    url: `/article-read/pass/quantity/${param.buzId}`,
    method: 'GET',
    header,
    data: param,
  });
  if (!data.data.success) {
    uni.showToast({
      title: data.data.message || '查询失败',
      duration: 2000,
      icon: 'none',
    });
    return Promise.reject(new Error(data.data.message || '查询失败'));
  }
  return data;
};

/**
 * 根据日期、业务获取阅读列表
 * buzId：业务id，路径传参， path 必传；
 * dateInt：格式：yyyyMMdd，非必传，默认当前日期 query
 * quantity：数量，非必传，默认3条
 */
export const getArticleSnapshot = async (param) => {
  let header = {
    auth: true,
  };
  const data = await http.request({
    baseUrl,
    url: `/article-read/list-articles/${param.buzId}`,
    header,
    method: 'GET',
  });
  if (!data.data.success) {
    uni.showToast({
      title: data.data.message || '获取阅读文章失败',
      duration: 2000,
      icon: 'none',
    });
    return Promise.reject(new Error(data.data.message || '获取阅读文章失败'));
  }
  return data;
};

/**
 * 阅读文章
 * buzId：业务id，路径传参，必传；
 * timestamp：unix时间戳，必传；
 * articleId：文章id，必传
 * index: 占位索引，必传
 */
export const read = async (param) => {
  let header = {
    auth: true,
    encryption: true,
  };
  const data = await http.request({
    baseUrl,
    url: `/article-read/read`,
    method: 'POST',
    header,
    data: param,
  });
  if (!data.data.success) {
    uni.showToast({
      title: data.data.message || '阅读文章失败',
      duration: 2000,
      icon: 'none',
    });
    return Promise.reject(new Error(data.data.message || '阅读文章失败'));
  }
  return data;
};
